package com.trade.common.utils;

/**
 * Description：
 *
 * @Author: Shaok Lei
 * @Date: 2016/3/16 18:29
 * @JDK: 1.8.0_40
 */

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author lsk
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    @SuppressWarnings("static-access")
    public void setApplicationContext(ApplicationContext ctx)
            throws BeansException {
        if (applicationContext == null) {
            this.applicationContext = ctx;
        }

    }

    public static void setAppctx(ApplicationContext ctx) {
        if (applicationContext == null) {
            applicationContext = ctx;
        }
    }

    @SuppressWarnings("unchecked")
    public static <T> T getBean(String beanId) {
        T obj = null;
        if (applicationContext != null) {
            obj = (T) applicationContext.getBean(beanId);
        }

        return obj;
    }

}
